#include "xshiftsetting.h"
#include "ui_xshiftsetting.h"

XShiftSetting::XShiftSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::XShiftSetting)
{
    ui->setupUi(this);
}

XShiftSetting::~XShiftSetting()
{
    delete ui;
}

QPair<double, double> XShiftSetting::getOption() {
    return {ui->spinStart->value(), ui->spinLength->value()};
}

void XShiftSetting::setOption(const XShift &shift, QCustomPlot *plotLma, QCustomPlot *plotLf) {
    ui->spinStart->setValue(shift.start);
    ui->spinStart->setSingleStep(shift.originLen * shift.factor);
    ui->spinLength->setValue(shift.length);
    ui->spinLength->setSingleStep(shift.originLen * shift.factor);
    mPlotLma = plotLma;
    mPlotLf = plotLf;
    mOriginLen = shift.originLen;
}

void XShiftSetting::shiftData(QCustomPlot *plotLma, QCustomPlot *plotLf, double start, double factor) {
    if (!plotLma || !plotLf) {
        return;
    }

    auto size = plotLma->graph(0)->dataCount();
    QVector<double> index(size);
    QVector<double> lma(size);
    QVector<double> lf(size);
    for (int i(0); i < size; ++i) {
        index[i] = i * factor + start;
        lma[i] = plotLma->graph(0)->data()->at(i)->mainValue();
        lf[i] = plotLf->graph(0)->data()->at(i)->mainValue();
    }
    plotLma->graph(0)->setData(index, lma, true);
    plotLf->graph(0)->setData(index, lf, true);
    plotLma->replot(QCustomPlot::rpQueuedReplot);
    plotLf->replot(QCustomPlot::rpQueuedReplot);

}

void XShiftSetting::on_btnDefault_clicked()
{
    ui->spinStart->setValue(0);
    ui->spinLength->setValue(mOriginLen);
    accept();
}

